package com.scap.authen.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.scap.authen.entity.UserLogin;


@Service
@Repository("voUserLoginDao")
public class UserLoginDaoImpl extends AbstractDao implements UserLoginDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getAllUserLogin() {
		Criteria criteria = getSession().createCriteria(UserLogin.class);
		List<Object> voUserLoginsList = (List<Object>)criteria.list();
		return voUserLoginsList;
	}
	
	public UserLogin getUserLoginByUserId(String vaUserId){
		Criteria criteria = getSession().createCriteria(UserLogin.class);
		criteria.add(Restrictions.eq("vaUserId",vaUserId));
		return (UserLogin)criteria.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getUserLoginLastUpdate() {
		Criteria criteria = getSession().createCriteria(UserLogin.class);
		criteria.addOrder(Order.desc("vaUpdateDate"));
		criteria.addOrder(Order.desc("vaUpdateTime"));
		criteria.addOrder(Order.desc("vaInsertDate"));
		criteria.addOrder(Order.desc("vaInsertTime"));
		criteria.setMaxResults(10);
		List<Object> voUserLoginsList = (List<Object>)criteria.list();
		shutdownSession();
		return voUserLoginsList;
	}

	@Override
	public boolean insertUserLogin(UserLogin voUserLogin) {
		boolean result = false;
		
		try {
			persist(voUserLogin);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		
		return result;
	}

	@Override
	public boolean updateUserLogin(UserLogin voUserLogin) {
		boolean result = false;
		
		try {
			
			Query query = getSession().createQuery("update UserLogin set vaName = :vaName ,"
					+ "vaPosition = :vaPosition ,"
					+ "vaEmailAddress = :vaEmailAddress,"
					+ "vaUserName = :vaUserName ,"
					+ "vaStatus = :vaStatus,"
					+ "vaUpdateDate = :vaUpdateDate ,"
					+ "vaUpdateTime = :vaUpdateTime,"
					+ "vnUpdateUserId = :vnUpdateUserId"
    				+ " where vaUserId = :vaUserId");
			
			query.setString("vaName", voUserLogin.getVaName());
			query.setString("vaPosition", voUserLogin.getVaPosition());
			query.setString("vaEmailAddress", voUserLogin.getVaEmailAddress());
			query.setString("vaUserName", voUserLogin.getVaUserName());
			query.setString("vaStatus", voUserLogin.getVaStatus());
			query.setString("vaUpdateDate", voUserLogin.getVaUpdateDate());
			query.setString("vaUpdateTime", voUserLogin.getVaUpdateTime());
			query.setInteger("vnUpdateUserId", voUserLogin.getVnUpdateUserId());
			query.setString("vaUserId", voUserLogin.getVaUserId());
			query.executeUpdate();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		
		return result;
	}

	@Override
	public boolean deleteUserLogin(UserLogin voUserLogin) {
		boolean result = false;
		
		try {
			delete(voUserLogin);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		
		return result;
	}

	



	
	
}
